package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: RoleController
 * @Author: jinghao
 * @Date: 2020/7/14 21:13
 * @Description:
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @RequestMapping("/addRole")
    public Result addRole(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds ){
        try {
            /*Role role = (Role) map.get("role");
            Integer[] menuIds = (Integer[]) map.get("menuIds");
            Integer[] permissionIds = (Integer[]) map.get("permissionIds");*/
            roleService.addRole(role,permissionIds,menuIds);
            return new Result(true, "添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }
    }
    @RequestMapping("/update")
    public Result update(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.update(role,permissionIds,menuIds);
            return new Result(true, "修改角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改角色失败");
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = roleService.findPage(queryPageBean);
        return page;
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true, "删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
    }
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        try {
            List<Menu> menus = roleService.findAllMenu();
            return new Result(true, "查询菜单成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }
    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
        try {
            List<Permission> permissions = roleService.findAllPermission();
            return new Result(true, "查询权限成功",permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }
    }
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        try {
            Role role = roleService.findRoleById(id);
            return new Result(true, "查询角色成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }
    @RequestMapping("/findMidsByRid")
    public Result findMidsByRid(Integer id){
        try {
            Integer[] mids = roleService.findMidsByRid(id);
            return new Result(true, "查询成功",mids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
    @RequestMapping("/findPidsByRid")
    public Result findPidsByRid(Integer id){
        try {
            Integer[] pids = roleService.findPidsByRid(id);
            return new Result(true, "查询成功",pids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    //role数据回显
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
